﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using ModelLibrary;

namespace WebC
{
    public partial class RepairApplist : clsBaseForm
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            topage.iRecordNum = iPageRowNum;
            btnDele.Attributes["onclick"] = "return deledata()";
            //btnEdit.Attributes["onclick"] = "return editdata('RepairApp.aspx')";
            btnEdit.Attributes["onclick"] = "return editdataNew('RepairApp.aspx')";
            btnAdd.Attributes["onclick"] = "return goaddurl('RepairApp.aspx')";
            btnSelect.Attributes["onclick"] = "return editdata('select.aspx')";
            if (!IsPostBack)
            {
                loadInfo();
            }
        }

        int iPageId
        {
            get
            {
                return Convert.ToInt32(Request["page"]) == 0 ? 1 : Convert.ToInt32(Request["page"]);
            }
        }


        public bool bIsEdit = false;
        public bool bIsView = false;

        public void loadInfo()
        {
            BuyDevice buy = null;
            DeviceInfo deviceinfo = new DeviceInfo();
            string sFullName = "";
            string sWhere = @"";
            ddlColName.SelectedValue = Convert.ToString(Request["colname"]);
            ddlType.SelectedValue = Convert.ToString(Request["type"]);
            txtWord.Text = Convert.ToString(Request["word"]);
            int iDeptId = Convert.ToInt32(Request["deptid"]);
            if (txtWord.Text != "")
            {
                switch (ddlColName.SelectedValue)
                {
                    default:
                        if (ddlType.SelectedValue == "like")
                        {
                            sWhere = ddlColName.SelectedValue + " " + ddlType.SelectedValue + "'%" + txtWord.Text + "%'";
                        }
                        else
                        {
                            sWhere = ddlColName.SelectedValue + " " + ddlType.SelectedValue + "'" + txtWord.Text + "'";
                        }
                        break;
                }
            }
            try
            {
                buy = new BuyDevice();
                if (!checkModelRightById(151, ref sFullName, (clsBaseLib)buy))
                {
                    errorUrl(string.Format("你没有“{0}”的权限,请与管理员联系！", sFullName));
                    return;
                }
                sModelFullName = sFullName;
                bIsView = checkModelRightById(152, ref sFullName, (clsBaseLib)buy);
                btnAdd.Visible = checkModelRightById(153, ref sFullName, (clsBaseLib)buy);
                btnDele.Visible = checkModelRightById(154, ref sFullName, (clsBaseLib)buy);
                bIsEdit = checkModelRightById(155, ref sFullName, (clsBaseLib)buy);
                btnEdit.Visible = bIsEdit;
                btnPlan.Visible = checkModelRightById(156, ref sFullName, (clsBaseLib)buy);
                //btnSelect.Visible = checkModelRightById(216, ref sFullName, (clsBaseLib)buy);
                //rpttblInfo.DataSource = deviceinfo.getRespairAppList11(iPageId, iDeptId, sWhere);
                //rpttblInfo.DataBind();
                DataGridMess.DataSource = deviceinfo.getRespairAppList(iPageId, iDeptId, sWhere);
                DataGridMess.DataBind();
                int iRecourdCount = Request["record"] != null ? Convert.ToInt32(Request["record"]) : buy.getDeviceCount(iDeptId, sWhere);
                topage.iRecordCount = iRecourdCount;
                topage.sPageUrl = string.Format("RepairApplist.aspx?colname={0}&type={1}&word={2}&record={3}&deptid={4}", HttpUtility.UrlEncodeUnicode(ddlColName.SelectedValue)
                    , HttpUtility.UrlEncodeUnicode(ddlType.SelectedValue), HttpUtility.UrlEncodeUnicode(txtWord.Text)
                    , iRecourdCount, iDeptId);
            }
            catch (Exception e)
            {
                errorUrl(Convert.ToString(e));
            }
            finally
            {
                if (buy != null)
                {
                    buy.Dispose();
                }
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            Response.Redirect(string.Format("RepairApplist.aspx?colname={0}&type={1}&word={2}&deptid={3}", ddlColName.SelectedValue
                    , ddlType.SelectedValue, txtWord.Text, Convert.ToInt32(Request["deptid"])));
        }
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Response.Redirect(string.Format("RepairApp.aspx?deptid={0}", Convert.ToInt32(Request["deptid"])));
        }
        protected void btnDele_Click(object sender, EventArgs e)
        {
            BuyDevice buy = null;
            DeviceInfo deviceinfo = new DeviceInfo();
            bool bResult = false;
            try
            {
                buy = new BuyDevice();
                //bResult = buy.deleteDeviceByIds(hdids.Value);
                bResult = deviceinfo.deleteRepairAppByIds(hdids.Value);
            }
            catch (Exception ex)
            {
                errorUrl(ex.ToString());
            }
            finally
            {
                if (buy != null)
                {
                    buy.Dispose();
                }
            }
            if (bResult)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "delesuc", string.Format("<script>alert('删除成功！');window.location=\"{0}\"</script>", Request.Url));
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "delesuc", "<script>alert('删除失败！');</script>");
            }
        }
        protected void btnEdit_Click(object sender, EventArgs e)
        {
            Response.Redirect(string.Format("RepairApp.aspx?id={0}", hdids.Value));
        }
        protected void btnSelect_Click(object sender, EventArgs e)
        {
            int i = 0;
            int j = 5 / i;
            Response.Redirect(string.Format("select.aspx?id={0}&rurl=Device", hdids.Value));
        }
        protected void DataGridMess_ItemCommand(object source, DataGridCommandEventArgs e)
        {

        }
        protected void DataGridMess_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {

        }
        protected void DataGridMess_SelectedIndexChanged(object sender, EventArgs e)
        {
            string ss = DataGridMess.SelectedIndex.ToString();
        }
        protected void DataGridMess_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            if (e.Item.ItemIndex < 0) return;
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                //e.Item.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='48d1cc'");
                //e.Item.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor");
                //e.Item.Attributes.Add("OnClick", "this.style.backgroundColor='#98D0FF'");
                e.Item.Attributes.Add("onclick", "if(window.oldtr!=null){window.oldtr.runtimeStyle.cssText='';}this.runtimeStyle.cssText='background-color:#98D0FF';window.oldtr=this;");
                string ss = bIsEdit ? string.Format("ondblclick=openWindow('{0}device/RepairApp.aspx?id={1}&deptid={2}')", sBaseUrl, e.Item.Cells[0].Text.Trim(), Convert.ToInt32(Request["deptid"])) : "";
                e.Item.Attributes.Add("ondblclick", ss);

                CheckBox cb = (CheckBox)e.Item.Cells[1].Controls[1];

                cb.Attributes.Add("onclick", "writeid('" + e.Item.Cells[0].Text.Trim() + "','',document.forms[0]." + cb.ClientID + ".checked," + e.Item.ItemIndex + ")");
            }

        }
        protected void btnPlan_Click(object sender, EventArgs e)
        {

        }
    }

}